package com.douya.pangu.application.basic.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.douya.common.pojo.dto.PagerDTO;
import com.douya.common.pojo.vo.CommonResponseVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.douya.pangu.application.basic.service.CoreUserService;
import com.douya.pangu.core.api.sys.pojo.dto.CoreUserDTO;
import com.douya.pangu.core.api.sys.pojo.vo.CoreUserVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 说明：核心用户表内部接口实现
 *
 * @author hcy
 * @date 2022/11/28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/web/basic/coreUser")
public class CoreUserController {

    @Resource
    CoreUserService mCoreUserService;

    /**
     * 分页获取核心用户表
     *
     * @param pager 分页条件
     * @return 核心用户表的分页信息
     */
    @GetMapping(value = "page")
    public CommonResponseVO<IPage<CoreUserVO>> page(@RequestParam("pager") PagerDTO pager) {
        return CommonResponseVO.success(mCoreUserService.page(pager));
    }

    /**
     * 根据ID获取核心用户表
     *
     * @param id ID
     * @return 核心用户表
     */
    @GetMapping(value = "findById")
    public CommonResponseVO<CoreUserVO> findById(@NotNull(message = "ID不能为空") Long id) {
        return CommonResponseVO.success(mCoreUserService.findById(id));
    }

    /**
     * 获取核心用户表
     *
     * @return 核心用户表
     */
    @GetMapping(value = "findAllUser")
//    @PreAuthorize("hasAnyAuthority('web:home')")
    public CommonResponseVO<List<CoreUserVO>> findAllUser() {
        return CommonResponseVO.success(mCoreUserService.findAllUser());
    }

    /**
     * 新增/修改核心用户表
     *
     * @param coreUserDTO 核心用户表
     */
    @PostMapping(value = "insert")
    public CommonResponseVO<?> insert(@Validated @RequestBody CoreUserDTO coreUserDTO) {
        mCoreUserService.insert(coreUserDTO);
        return CommonResponseVO.success();
    }

    /**
     * 根据ID删除核心用户表
     *
     * @param id ID
     */
    @DeleteMapping(value = "delete")
    public CommonResponseVO<?> delete(@NotNull(message = "ID不能为空") Long id) {
        mCoreUserService.delete(id);
        return CommonResponseVO.success();
    }
}
